package baidu.ui.containers
{
    import baidu.ui.constants.*;
    import baidu.ui.core.*;
    import baidu.ui.events.*;
    import flash.display.*;

    public class Box extends ContainerBase
    {
        protected var _direction:String;
        public static var defaultStyles:Object = {padding:5, gap:5};

        public function Box()
        {
            return;
        }

        override public function get classStyles() : Object
        {
            return mergeStyles(super.classStyles, defaultStyles);
        }

        override public function addChild(param1:DisplayObject) : DisplayObject
        {
            super.addChild(param1);
            param1.addEventListener(BUIEvent.SIZE_CHANGE, this.childResizeHandler);
            invalidate(Invalidation.LAYOUT);
            return param1;
        }

        override public function addChildAt(param1:DisplayObject, param2:int) : DisplayObject
        {
            super.addChildAt(param1, param2);
            param1.addEventListener(BUIEvent.SIZE_CHANGE, this.childResizeHandler);
            invalidate(Invalidation.LAYOUT);
            return param1;
        }

        override public function removeChild(param1:DisplayObject) : DisplayObject
        {
            super.removeChild(param1);
            param1.removeEventListener(BUIEvent.SIZE_CHANGE, this.childResizeHandler);
            invalidate(Invalidation.LAYOUT);
            return param1;
        }

        override public function removeChildAt(param1:int) : DisplayObject
        {
            var _loc_2:* = super.removeChildAt(param1);
            _loc_2.removeEventListener(BUIEvent.SIZE_CHANGE, this.childResizeHandler);
            invalidate(Invalidation.LAYOUT);
            return _loc_2;
        }

        override public function swapChildren(param1:DisplayObject, param2:DisplayObject) : void
        {
            invalidate(Invalidation.LAYOUT);
            super.swapChildren(param1, param2);
            return;
        }

        override public function swapChildrenAt(param1:int, param2:int) : void
        {
            invalidate(Invalidation.LAYOUT);
            super.swapChildrenAt(param1, param2);
            return;
        }

        protected function childResizeHandler(event:BUIEvent) : void
        {
            invalidate(Invalidation.LAYOUT);
            return;
        }

        override protected function draw() : void
        {
            if (isInvalid(Invalidation.LAYOUT, Invalidation.STYLES, Invalidation.SIZE))
            {
                this.drawLayout();
                dispatchEvent(new BUIEvent(BUIEvent.SIZE_CHANGE));
            }
            super.draw();
            if (isInvalid(Invalidation.SIZE))
            {
                trace(this.height);
            }
            return;
        }

        protected function drawLayout() : void
        {
            var _loc_1:DisplayObject = null;
            var _loc_2:* = getStyleValue("padding");
            var _loc_3:int = 0;
            while (_loc_3 < this.numChildren)
            {
                
                _loc_1 = getChildAt(_loc_3);
                if (_loc_1 is BUI)
                {
                    (_loc_1 as BUI).drawNow();
                }
                if (this._direction == BUIConstants.VERTICAL)
                {
                    _loc_1.y = _loc_2;
                    _loc_2 = _loc_2 + (_loc_1 is BUI ? ((_loc_1 as BUI).spriteHeight) : (_loc_1.height));
                    _loc_2 = _loc_2 + getStyleValue("gap");
                }
                else if (this._direction == BUIConstants.HORIZONTAL)
                {
                    _loc_1.x = _loc_2;
                    _loc_2 = _loc_2 + (_loc_1 is BUI ? ((_loc_1 as BUI).spriteWidth) : (_loc_1.width));
                    _loc_2 = _loc_2 + getStyleValue("gap");
                }
                _loc_3++;
            }
            if (this._direction == BUIConstants.VERTICAL)
            {
                super.height = _loc_2 - getStyleValue("gap") + getStyleValue("padding");
                super.width = this.getBounds(this.parent).width;
            }
            else if (this._direction == BUIConstants.HORIZONTAL)
            {
                super.width = _loc_2 - getStyleValue("gap") + getStyleValue("padding");
                super.height = this.getBounds(this.parent).height;
            }
            dispatchEvent(new BUIEvent(BUIEvent.SIZE_CHANGE));
            return;
        }

        public function get direction() : String
        {
            return this._direction;
        }

        public function set direction(param1:String) : void
        {
            this._direction = param1;
            invalidate(Invalidation.LAYOUT);
            return;
        }

    }
}
